
(function( $ ) {
	
	$.fn.lifequestAPI = function(JSONRequest,successFunc) {
	
    	$.ajax({
    		url: "./php/api.php",
    		type: "POST",
    		cache: false,
    		data: {query : JSONRequest},
    		success: function(result){ 
    				var jsonResponse = null;
    				try {
	    				jsonResponse = jQuery.parseJSON(result);
	    			} catch(e) {
    					alert("Error:" + result);
    				//console.log(result);
	    			}
    				successFunc(jsonResponse)
    			},
    		error: function(jqXHR, textStatus, errorThrown){
    			alert("Error:" + textStatus);
    		}/*,
    		dataType: "json"*/
    	});

		return this;
	
	};

})(jQuery);

function getQuests($user_id) {
	
	$().lifequestAPI({
		"request":"get_quests",
		"user_id": $user_id
	}, function(result) {
		var index = 0;
		$.each(result, function(i, item) {
			index ++;
			$("#tb_questlist").append(
				"<tr class='success' onclick='select_quest(this);' id='" + item["quest_id"] + "'>" +
				"<td>" + index + "</td>" + 
				"<td>" + item["quest_nm"] + "</td>" + 
				"<td>" + item["quest_type"] + "</td>" +
				"<td>" + item["quest_start_date"] + "</td>" +
				"<td>" + item["quest_exp_date"] + "</td>" +
				"<td>" + item["quest_freq"] + "</td>" +
				"<td>" + item["last_chk_date"] + "</td>" +
				"</tr>");
		});
	});
}

function select_quest(o) {
		
	var myForm = document.createElement("form");
	myForm.method = "post";
	myForm.action = "quest.php";
	
	var inputId = document.createElement("input");
	inputId.setAttribute("name", "questid");
	inputId.setAttribute("value", o.id);
	
	myForm.appendChild(inputId);
	document.body.appendChild(myForm);
	myForm.submit();
	document.body.removeChild(myForm);
}

function register_quest($user_id, $name, $type, $freq, $exp_date) {
    
	$().lifequestAPI({
		 "request":"register_quest",
		 "user_id":$user_id,
		 "quest_nm":$name,
		 "quest_type":$type,
		 "quest_freq":$freq,
		 "quest_exp_date":$exp_date
	 }, function(result) {
	     if(result['result'] == 'success') {
	         alert("퀘스트를 등록했습니다");
            window.location.href = './main.php';
	     } 
	 });
}

function delete_quest(id) {
	$().lifequestAPI({
		"request":"delete_quest",
		"quest_id":id
	}, function(result) {	  
	    if(result['result'] == 'success') {
            alert("퀘스트를 삭제했습니다");
            window.location.href = './main.php';
            	        
	    }
		
	});
}

function checkin(o, id) {
	
	if($(o).hasClass('disabled'))
		return;
		
	$().lifequestAPI({
		"request":"update_checkin",
		"quest_id":id
	}, function(result) {
	    if(result['result'] == 'success') {
            alert("체크인했습니다");
            //location.reload();
            
	    }
	});
}

function cancel_checkin(id) {
	$().lifequestAPI({
		"request":"cancel_checkin",
		"quest_id": id
	}, function(result) {
	    if(result['result'] == 'success') {
            alert("체크인을 취소했습니다");
        }
	});
}

function initiate_input(o) {
	$.each(o, function() {
		$(this).val("");
		if($(this).closest('.control-group').hasClass('error')) {
           $(this).closest('.control-group').removeClass('error');
        }
        
        if($(this).attr("data-content")) {
        	
        }
        else {
        }
        
	});
}

function validate_input(o) {
    $result = true;
    
    $.each(o, function() {
       if($(this).val().length == 0) {
           $(this).closest('.control-group').addClass('error');
           $result = false;
       }
       else if($(this).closest('.control-group').hasClass('error')) {
           $(this).closest('.control-group').removeClass('error');
        }
    });
    
    return $result;
}

function signup(o_nm, o_pw, success_func) {
	$result = validate_input([o_nm, o_pw]);
	if(!$result)
		return;
		
	$().lifequestAPI({
		"request":"signup",
		"user_nm": o_nm.val(),
		"user_pw": o_pw.val(),
	}, function(result) {
	    if(result['result'] == 'success') {
	    	success_func();
        }
        else
        {
        	switch(result['type']) {
        		case 'exist_name':
        		case 'short_name':
        			o_nm.closest('.control-group').addClass('error');
        			$('#input_su_nm').popover('show');
        			$("#input_su_nm").focus();
	        		break;
	        	case 'short_password':
	        		o_pw.closest('.control-group').addClass('error');
	        		$('#input_su_pw').popover('show');
	        		$("#input_su_pw").focus();
	        		break;	
        		default:
        			break;
        	}
        }
	});
		
}



